/*global jQuery: false */

(function( $ ) {
	"use strict";
	
	var methods = {},
		privateMethods = {},
		settings = {},
		workingData = {};
	
	privateMethods = {
		initialize: function( hero ) {
			workingData.hero = hero;
			workingData.heroHeight = hero.attr("data-image-height");
			workingData.heroTitleHeight = hero.find("h1").height();
			workingData.heroTextBox = hero.find(".textBox");
			workingData.heroIntervalId = null;
			workingData.carouselItems = hero.find(".destinationhero .slideshow .carouselItem");
            workingData.navigatorItems = hero.find(".destinationhero .navigatorItem");

			hero.height(workingData.heroHeight);
			workingData.heroTextBox.height( workingData.heroTitleHeight );
		},

        switchImage: function() {

            privateMethods.navigate();
        },
		
		bindEvents: function() {
			workingData.heroTextBox.hover( privateMethods.handleOnHoverTextBox, privateMethods.handleHoverOutTextBox );
			workingData.navigatorItems.click( privateMethods.handleOnClickNavigatorItem );
		},

        handleOnClickNavigatorItem: function( event ) {
            event.preventDefault();
            privateMethods.navigate(event);
        },

        navigate: function( event ) {

            if(typeof event !== "undefined"){
                event.preventDefault();
            }

            var actualNavigatorItem = $(this),
                activeImage,
                notActiveImages,
                activeNavigator = workingData.navigatorItems.filter(".active"),
                activeCarouselItem = workingData.carouselItems.filter(".active"),
                number = workingData.carouselItems.index( activeCarouselItem),
                numberOfCarouselItems = workingData.carouselItems.length;

            clearInterval( workingData.heroIntervalId );

            if(actualNavigatorItem.hasClass('prev')){
                number = ( parseInt(number,10) - 1 ) % numberOfCarouselItems;

            }
            else {
                number = ( parseInt(number,10) + 1 ) % numberOfCarouselItems;
            }

            activeImage = workingData.carouselItems.eq( number);
            notActiveImages = workingData.carouselItems.not(activeImage);

            activeImage.stop(true, true).fadeIn( 2000 ).addClass( "active" );
            notActiveImages.stop(true, true).fadeOut( 2000 ).removeClass( "active" );

            privateMethods.rotateIfAutoPlayOn();
        },
		
		handleOnHoverTextBox: function() {
			var description, newHeight, textBox;
			
			textBox = $(this);
			
			description = textBox.children(".description");
			description.stop(true, true).show();

			newHeight = Math.min( workingData.heroHeight, workingData.heroTitleHeight + description.height() );
			textBox.stop(true, true).animate({ height: newHeight, opacity: 0.8 }, 600);
			description.css("opacity", "1");
		},
		
		handleHoverOutTextBox: function() {
			var textBox = $(this);
			
			textBox.animate({ height: workingData.heroTitleHeight, opacity: 1 }, 600);
			textBox.children(".description").stop(true, true).fadeOut( 700 );
		},
		
		rotateIfAutoPlayOn: function() {
			if ( settings.autoPlay ) {
				 workingData.heroIntervalId = setInterval( privateMethods.switchImage, settings.rotationSpeed );				 
			}
		}
	};
	
	methods = {
		init: function( options ) {
			$.extend( settings, $.fn.tcDestinationHero.defaults, options );
			
			return this.each(function() {
				var hero = $(this);
				
				privateMethods.initialize( hero );
				
				privateMethods.bindEvents();
				
				privateMethods.rotateIfAutoPlayOn();
			});
		}
	};
	
	$.fn.tcDestinationHero = function ( method ) {
		// Method calling logic
	    if ( methods[method] ) {
	      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } 
	    
	    if ( typeof method === 'object' || ! method ) {
	      return methods.init.apply( this, arguments );
	    } 
	    
	    $.error( 'Method ' +  method + ' does not exist on jQuery.tcHeroCarousel plug-in.' );
	};
	
	$.fn.tcDestinationHero.defaults = {
		rotationSpeed: 5000,
		autoPlay: true
	};
	
} ( jQuery ));



